<template>
  <div>
    <div
      :class="$route.name == 'imageManagement' ? 'imageManagement' : ''"
      class="image-video-con flex-center-start"
    >
        <imageUnit
          class="image-unit"
          v-for="(item, index) in imageVideoList"
          :key="index"
          :logo="item.thumbnail"
          :id="item.id"
          :fileName="item.fileName"
          :isChoosed="choosedUnitArr.indexOf(item.id) != -1"
          @click.native="unitClick(index, item)"
          @contactTarget="contactTarget"
        >
          <div class="operation flex-between" slot="btnCon" v-if="isEvaluate">
            <div
              class="evaluate flex-center"
              @click.stop="openEvaluateDialog(item.id)"
            >
              评价
            </div>
            <div class="audit flex-center" @click.stop="showTempAudit(item)">审核</div>
            <div class="audit flex-center" v-if="examining && item.isExamine === 1 && item.examineType != 2" @click.stop="audit(item.id, item.isExamine)">会审</div>
            <!-- <div class="audit flex-center" v-if="item.isExamine === 1" @click.stop="audit(item.id, 1)">会审</div> -->
            <div class="audit flex-center" v-else-if="examining && item.isExamine === 4" @click.stop="audit(item.id, item.isExamine)">终审</div>
          </div>
          <div class="operation share flex-between" slot="btnCon" v-if="isShare">
            <div class="audit flex-center" @click="storage(item)">
              {{ dataTypeTabsId == 2 ? "同步图片" : "同步视频" }}
            </div>
          </div>
          <div class="operation flex-between" slot="btnCon" v-if="isImage">
            <div class="delete flex-center" @click="deleteImage(index)">删除</div>
            <div class="edit flex-center" @click.stop="edit(item.id)">编辑</div>
          </div>
          <div class="time" slot="size" style="margin-right: 10px;" v-show="examining && (item.isExamine == 1 || item.isExamine == 4) && item.examineType != 2">
            <el-checkbox v-model="item.checked" @change.stop.native="checkboxChange" />
          </div>
          <div
            class="check"
            :class="getExamineClass(item.isExamine)"
            slot="checkStatus"
          >
            {{ getExamineType(item.isExamine, item.examineType) }}
          </div>
          <div class="time" slot="size" v-show="dataTypeTabsId == 2">
            {{ item.imageSize }}
          </div>
          <div class="time" slot="size" v-show="dataTypeTabsId == 3">
            {{ $byteConvert(item.fileSize) }}
          </div>

          <div
            class="imagePreview flex-center"
            slot="imagePreview"
            @click="goDetail(item.id)"
          >
            <el-tooltip effect="dark" content="查看详情" placement="bottom">
              <div class="icon"></div>
            </el-tooltip>
          </div>
        </imageUnit>
      <asideDetail
        :choosedItem="choosedItem"
        v-if="Object.keys(choosedItem).length > 1"
        @editTags="editTags"
        @reGetData="reGetData"
      ></asideDetail>
    </div>
    <evaluateDialog
      ref="evaluateDialog"
      @evaluateDialogConfirm="evaluateDialogConfirm"
    ></evaluateDialog>
    <!-- 同步页 用于查询目标名称 -->
    <synchronization ref="synchronization"></synchronization>
    <el-dialog title="审核" :visible.sync="tempAuditShow">
      <div>审核通过吗？</div>
      <div class="footer flex-center">
        <div class="cancel" @click="tempAudit(3)">不通过</div>
        <div class="confirm" @click="tempAudit(2)">通过</div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import imageUnit from "@/components/imageVideo/imageUnit.vue";
// 评价弹窗
import evaluateDialog from "@/components/common/evaluateDialog.vue";
import asideDetail from "@/components/imageVideo/asideDetail.vue";
import synchronization from "../common/synchronizationDialog";
import { mapState } from "vuex";
export default {
  components: {
    imageUnit,
    evaluateDialog,
    asideDetail,
    synchronization,
  },
  props: {
    imageVideoList: {
      default: () => [],
    },
    isImage: {
      default: () => false,
    },
    isEvaluate: {
      default: () => false,
    },
    isShare: {
      default: () => false,
    },
  },
  computed: {
    ...mapState({
      dataTypeTabsId(state) {
        return state.app.dataTypeTabsId;
      },
      targetTreeChecked(state) {
        return state.app.targetTreeChecked;
      },
      targetTreeClickId(state) {
        return state.app.targetTreeClickId;
      },
      user(state) {
        return state.user
      },
      examining(state) {
        return state.app.examining;
      },
      checkedDataIds(state) {
        console.log(state.app.checkedDataIds)
        return state.app.checkedDataIds;
      }
    }),
  },
  data() {
    return {
      choosedUnitArr: [],
      choosedItem: {
        categoryTypeList: [],
      }, // 当前选中Item
      isDetail: false, // 是否转到详情页,
      evaluteList: [], // 评价列表
      evaluateVideoList: [],
      tempItem: null,
      tempAuditShow: false,
      // checkedDataIds: [], // 批量审核选中成果数据IDS
    };
  },
  watch: {
    imageVideoList: {
      async handler() {
        if (this.choosedUnitArr.length == 0) {
          if (this.imageVideoList.length > 0) {
            this.choosedUnitArr = [this.imageVideoList[0].id];
          }
        }
        this.choosedItem = await this.getAsideItem(this.choosedUnitArr[0]);
      },
      deep: true,
    },
    dataTypeTabsId() {
      this.choosedUnitArr = [];
    },
  },
  methods: {
    getExamineType(type,type2) {
      if(type == 1) {
        if (type2 == 1) {
          return '未完成'
        } else if (type2 ==2) {
          return '已完成'
        } else {
          return '未审核'
        }
      }else if (type == 2) {
        return '审核通过'
      }else if (type == 3) {
        return '废弃'
      }else if (type ==4) {
        return '存疑'
      }
    },
    getExamineClass(type) {
      if(type == 1) {
        return 'examine1'
      }else if (type == 2) {
        return 'examine2'
      }else if (type == 3) {
        return 'examine3'
      }else if (type ==4) {
        return 'examine4'
      }
    },
    showTempAudit(item){
        this.tempItem = item;
        this.tempAuditShow = true;
    },
    hideTempAudit(){
        this.tempItem = null;
        this.tempAuditShow = false;
    },
    tempAudit(isPass){
       this.$api
       .tempExamine({
         id: this.tempItem.id,
         fileType: this.dataTypeTabsId,
         examineType: isPass,
       })
       .then((res) => {
         this.hideTempAudit()
         if (res.indexOf("200") != -1) {
           this.$message({
             type: "success",
             message: "审核成功！",
             showClose: true
           });
           this.reGetData()
         } else {
           this.$notify({
             title: "提示",
             message: '操作失败',
             type: "warning",
           });
         }
       });
    },
    // 批量审核选中
    async checkboxChange() {
      if (this.imageVideoList.length > 0) {
        let checkedDataIds = []
        await this.imageVideoList.forEach(item => {
          if (item.checked) {
            checkedDataIds.push(item.id)
          }
        });
        this.$store.commit('app/setCheckedDataIds', checkedDataIds.join(","))
      }
    },
    // 目标关联数据
    contactTarget(id) {
      if (this.targetTreeClickId == "") {
        this.$message({
          type: "warning",
          message: "请先选择目标！",
          showClose: true
        });
        return false;
      }
      this.$api
        .targetJoinData({
          id: id,
          dataType: this.dataTypeTabsId,
          zbnm: this.targetTreeClickId,
        })
        .then((res) => {
          if (res.indexOf("200") != -1) {
            this.$message({
              type: "success",
              message: "关联成功！",
              showClose: true
            });
          } else {
            this.$notify({
              title: "提示",
              message: res.split(",")[1],
              type: "warning",
            });
          }
        });
    },
    // 重新刷新接口
    reGetData() {
      this.$emit("reGetData");
    },
    // 获取右边详情数据
    async getAsideItem(id) {
      // 改为请求接口获取右边详情信息
      let data = await this.$api.singleInfo({
        type: this.dataTypeTabsId,
        id: id,
      });
      return new Promise((resolve) => {
        resolve(data);
      });
    },
    storage(item) {
      
      let flag = null;
      if (this.dataTypeTabsId === 2) {
        flag = 0;
      } else if (this.dataTypeTabsId === 3) {
        flag = 1;
      }
      this.$refs.synchronization.openDialog(item.id, flag);
      /* }
      this.$api
        .shareZCData({
          dataIds: this.dataTypeTabsId == 1 ? this.choosedUnitArr : [],
          imageIds: this.dataTypeTabsId == 2 ? this.choosedUnitArr : [],
          targetId: item.targetType,
          videoIds: this.dataTypeTabsId == 3 ? this.choosedUnitArr : [],
        })
        .then((res) => {
          window.open(window.VUE_APP_BASE_URL + "/" + res);
        }); */
    },
    deleteImage(index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        showClose: false,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .deleteImage({
              ids: [...this.choosedUnitArr],
              type:
                this.dataTypeTabsId == 2
                  ? "image"
                  : this.dataTypeTabsId == 3
                  ? "video"
                  : "data",
            })
            .then(() => {
              this.$emit("deleteSuccess", index);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            showClose: true
          });
        });
    },
    async edit(id) {
      let item = await this.getAsideItem(id);
      this.$router.push({
        name: "imagesVideoAddTable",
        params: {
          selectedTabId: this.dataTypeTabsId,
          item: item,
          isEdit: true,
        },
      });
    },
    // 审核
    async audit(id, examineType) {
      let type;
      if (examineType == 1) {
        type = 1
      } else if (examineType == 4) {
        type = 2
      }
      let item = await this.getAsideItem(id);
      this.$store.commit("app/setFinalCheck", type)
      this.$router.push({
        name: "imagesVideoAddTable",
        params: {
          selectedTabId: this.dataTypeTabsId,
          item: item,
          isAudit: true,
        },
      });
    },
    async goDetail(id) {
      let item = await this.getAsideItem(id);
      this.evaluteList = [];
      if (this.dataTypeTabsId == 2) {
        this.$api.imageEvaluate({ id: item.id }).then((res) => {
          this.evaluteList = res.imageEvaluateList;
          this.$router.push({
            name: "imageVideoDetail",
            params: {
              item: item,
              list: this.evaluteList,
            },
          });
        });
      } else if (this.dataTypeTabsId == 3) {
        this.$api.videoEvaluate({ id: item.id }).then((res) => {
          this.evaluteVideoList = res.videoEvaluateList;
          this.$router.push({
            name: "imageVideoDetail",
            params: {
              item: item,
              videoList: this.evaluteVideoList,
            },
          });
        });
      }
    },
    // 评论弹窗
    evaluateDialogConfirm({ value, text }) {
      this.$api
        .savedatatEvaluate({
          connectionType: this.dataTypeTabsId,
          evaluate: text,
          score: value,
          userId: this.user.userId,
          id: this.choosedItem.id,
        })
        .then(() => {
          this.$message({
            type: "success",
            message: "评价成功!",
            showClose: true
          });
        });
    },
    // 打开评论弹窗
    openEvaluateDialog() {
      this.$refs.evaluateDialog.openDialog();
    },
    async unitClick(index, item) {
      //  改为单选
      this.choosedUnitArr = [];
      this.choosedUnitArr.push(item.id);
      this.choosedItem = await this.getAsideItem(item.id);
    },
    // 全选方法
    /* allSelected() {
      if (this.choosedUnitArr.length == this.imageVideoList.length) {
        this.choosedUnitArr = [];
        return false;
      }
      this.choosedUnitArr = this.imageVideoList.map((item) => {
        return item.id;
      });
    }, */
    editTags(e) {
      this.$emit("editTags", e);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
.image-video-con {
  // background: pink;
  // padding-bottom: 20px;
  padding-right: 0.1rem;
  @include scrollBar;
  height: calc(100vh - 2rem);
  &.imageManagement {
    height: calc(100vh - 2.4rem);
  }
  overflow: auto;
  flex-wrap: wrap;
  align-items: flex-start;
  align-content: flex-start;
  .nodata {
    width: 100%;
    color: #ccc;
    opacity: 0.5;
    margin-top: 100px;
    text-align: center;
  }
  .image-unit {
    position: relative;
    .operation {
      // background: pink;
      color: white;
      position: absolute;
      z-index: 100;
      top: 0.12rem;
      width: 100%;
      // padding: 0 0.1rem;
      padding-left: 2%;
      padding-right: 5%;
      font-size: 0.14rem;
      & > div {
        cursor: pointer;
      }
      .delete {
        width: 0.54rem;
        height: 0.28rem;
        background: #e64f4f;
        border-radius: 0.05rem;
      }
      .edit {
        width: 0.54rem;
        height: 0.28rem;
        background: #fda53e;
        border-radius: 5px;
      }
      .evaluate {
        width: 0.54rem;
        height: 0.28rem;
        background: #e64f4f;
        border-radius: 0.05rem;
      }
      .audit {
        cursor: pointer;
        padding: 0 0.06rem;
        // width: 0.54rem;
        height: 0.28rem;
        background: rgba(24, 144, 255, 1);
        border-radius: 0.05rem;
      }
      &.share {
        justify-content: flex-end;
      }
    }
    .check {
      margin-left: 0.2rem;
      font-size: 0.14rem;
      
      &.isExamine {
        color: rgba(55, 199, 76, 1);
      }
      &.examine1 {
        color: #409EFF;
      }
      &.examine2 {
        color: #67C23A;
      }
      &.examine3 {
        color: #F56C6C;
      }
      &.examine4 {
        color: #E6A23C;
      }
    }
    .imagePreview {
      content: "";
      position: absolute;
      width: 0.4rem;
      height: 0.4rem;
      background: rgba(0, 0, 0, 0.3);
      border-radius: 0.05rem;
      bottom: 0.76rem;
      right: 0.16rem;
      cursor: pointer;
      .icon {
        width: 100%;
        height: 100%;
        background-image: url(~@/assets/images/enlarge@2x.png);
        background-repeat: no-repeat;
        background-size: 0.22rem 0.22rem;
        background-position: center;
      }
    }
  }
  .aside-detail {
    width: 2.87rem;
    position: absolute;
    right: 0;
    bottom: 0;
    height: calc(100vh - 0.76rem);
    // background: #404040;
    & > div {
      width: 2.4rem;
      margin: 0 auto;
    }
    .logo {
      // background-image: url(~@/assets/common/20.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-position: center;
      margin-top: 0.53rem;
      height: 1.6rem;
      border-radius: 0.1rem;
    }
    .title {
      margin-top: 0.12rem;
      font-size: 0.14rem;
      font-family: Alibaba PuHuiTi;
      font-weight: 400;
      color: #ffffff;
    }
    .des {
      margin-top: 0.1rem;
      font-size: 0.14rem;
      font-family: Alibaba PuHuiTi;
      font-weight: 400;
      color: #bbbbbb;
    }
    .tags {
      margin-top: 0.2rem;
      width: 2.4rem;
      height: 1rem;
      background: #383838;
      border-radius: 0.05rem;
    }
    .text-con {
      .tit {
        margin-top: 0.2rem;
        font-size: 0.14rem;
        font-weight: 400;
        color: #ffffff;
      }
      .params {
        margin-top: 0.1rem;
        font-size: 0.14rem;
        font-weight: 400;
        color: #ffffff;
        opacity: 0.67;
      }
    }
  }
}

::v-deep .right-menu {
  position: absolute;
  cursor: pointer;
  border: none;
  ul {
    border-radius: 0.08rem;
    color: #ffffff;
    padding: 0.1rem 0;
    // position: fixed;
    background: #0f2d43;
    width: 1.3rem;
    min-width: 1.3rem;
    font-size: 0.14rem;
    text-align: center;
  }
  li {
    text-align: center;
  }
  a {
    color: #ffffff;
  }
}
::v-deep .el-dialog {
  color: #ffffff;
  font-size: 0.16rem;
  width: 7.2rem;
  background: #474747;
  box-shadow: 0rem 0rem 0.38rem 0rem rgba(0, 0, 0, 0.45);
  border-radius: 0.05rem;
  .el-dialog__header {
    padding: 0.16rem;
    background: #143853;
  }
  .el-dialog__title {
    color: #ffffff;
  }
  .el-dialog__body {
    color: #ffffff;
    background: #0f2d43;
  }
  .footer {
    margin-top: 0.3rem;
    color: #ffffff;
    margin-right: 0.3rem;
    justify-content: flex-end;
    .cancel {
      text-align: center;
      width: 1.2rem;
      height: 0.38rem;
      line-height: 0.38rem;
      background: #62656c;
      border-radius: 0.05rem;
    }
    .confirm {
      margin-left: 0.2rem;
      text-align: center;
      width: 1.2rem;
      height: 0.38rem;
      line-height: 0.38rem;
      background: #1890ff;
      border-radius: 0.05rem;
    }
  }
}
</style>
